package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.service.ISysCityService;
import com.ruoyi.project.system.service.ISysGoodsService;
import com.ruoyi.project.system.service.ISysMobileUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysGoodsOrder;
import com.ruoyi.project.system.service.ISysGoodsOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 二手物品订单Controller
 *
 * @author jjp
 * @date 2020-04-24
 */
@RestController
@Api(description = "二手物品订单Controller")
@RequestMapping("/system/goodsOrder")
public class SysGoodsOrderController extends BaseController {
    @Autowired
    private ISysGoodsOrderService sysGoodsOrderService;

    @Autowired
    private ISysCityService sysCityService;

    @Autowired
    private ISysGoodsService sysGoodsService;

    @Autowired
    private ISysMobileUserService sysMobileUserService;

    /**
     * 查询二手物品订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @ApiOperation("查询二手物品订单列表")
    @GetMapping("/list")
    public TableDataInfo list(SysGoodsOrder sysGoodsOrder) {
        startPage();
        List<SysGoodsOrder> list = sysGoodsOrderService.selectSysGoodsOrderList(sysGoodsOrder);
        for (int i = 0; i < list.size(); i++) {
            Long goodsOrderId = list.get(i).getGoodsOrderId();
            SysGoodsOrder goodsOrder = sysGoodsOrderService.selectSysGoodsOrderById(goodsOrderId);
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(goodsOrder.getMobileUserId());
            list.get(i).setPhone(sysMobileUser.getPhone());
        }
        return getDataTable(list);
    }

    /**
     * 获取二手物品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("获取二手物品订单详细信息")
    @GetMapping(value = "/{goodsOrderId}")
    public AjaxResult getInfo(@PathVariable("goodsOrderId") Long goodsOrderId) {
        SysGoodsOrder sysGoodsOrder = sysGoodsOrderService.selectSysGoodsOrderById(goodsOrderId);
        Long area = Long.valueOf(sysGoodsOrder.getArea());
        SysCity sysCity = sysCityService.selectSysCityById(area);
        String cityName = sysCity.getCityName();
        sysGoodsOrder.setArea(cityName);
        SysGoods sysGoods = sysGoodsService.selectSysGoodsById(sysGoodsOrder.getGoodsId());
        sysGoodsOrder.setGoodsName(sysGoods.getGoodsName());
        SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysGoodsOrder.getMobileUserId());
        sysGoodsOrder.setPhone(sysMobileUser.getPhone());
        return AjaxResult.success(sysGoodsOrder);
    }

    /**
     * 新增二手物品订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "二手物品订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGoodsOrder sysGoodsOrder) {
        return toAjax(sysGoodsOrderService.insertSysGoodsOrder(sysGoodsOrder));
    }

    /**
     * 修改二手物品订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @ApiOperation("修改二手物品订单")
    @Log(title = "二手物品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGoodsOrder sysGoodsOrder) {
        return toAjax(sysGoodsOrderService.updateSysGoodsOrder(sysGoodsOrder));
    }

    /**
     * 删除二手物品订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @ApiOperation("删除二手物品订单")
    @Log(title = "二手物品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsOrderIds}")
    public AjaxResult remove(@PathVariable Long[] goodsOrderIds) {
        return toAjax(sysGoodsOrderService.deleteSysGoodsOrderByIds(goodsOrderIds));
    }
}
